<template>
  <view class="ditail-max-box">
    <!-- 主图滚动区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="(item,i) in detailList.pics" :key="i">
        <image :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    <!-- 详情页区域 -->
    <view class="detail-box" v-if="detailList.goods_price">
      <!-- 商品价格 -->
      <view class="detail-price">￥{{detailList.goods_price}}</view>
      <!-- body内容头部 -->
      <view class="detail-body">
        <!-- 文字 -->
        <view class="detail-title">{{detailList.goods_name}}</view>
        <!-- 收藏区 -->
        <view class="detail-favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费区 -->
      <view class="yf">
        快递：免运费
      </view>
      <!-- 详情页区 -->
      <rich-text :nodes="detailList.goods_introduce"></rich-text>
      <!-- 购物车购买底部区 -->
      <uni-goods-nav class="goods-nav" :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapState('m_cart', []),
      ...mapGetters('m_cart', ['total'])
    },
    data() {
      return {
        detailList: {},
        options: [{
          icon: 'shop',
          text: '店铺',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    watch: {
      total: {
        handler(newval) {
          const findResult = this.options.find(item => item.text === '购物车')
          findResult.info = newval
        },
        immediate:true
      }
    },
    onLoad(options) {
      this.getDetail(options.goods_id)
    },
    methods: {
      ...mapMutations('m_cart', ['addtoCart', 'test']),
      async getDetail(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.detailList = res.message
        this.detailList.goods_introduce = this.detailList.goods_introduce.replace(/<img /g,
          '<img style="display:block;"').replace(/.webp/g, '.jpg')
      },
      preview(i) {
        uni.previewImage({
          current: i,
          urls: this.detailList.pics.map(item => item.pics_big)
        })
      },
      onClick(e) {
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        if (e.content.text === '加入购物车') {
          const goods = {
            goods_id: this.detailList.goods_id,
            goods_name: this.detailList.goods_name,
            goods_price: this.detailList.goods_price,
            goods_count: 1,
            goods_state: true,
            goods_small_logo: this.detailList.goods_small_logo
          }
          this.addtoCart(goods)
        }
      }
    }
  }
</script>

<style lang="scss">
  .ditail-max-box {
    padding-bottom: 50px;
  }

  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .detail-box {
    padding: 10px;

    .detail-price {
      color: #C00000;
      padding: 10px 0;
    }

    .detail-body {
      display: flex;
      justify-content: space-between;

      .detail-title {
        font-size: 13px;
        margin-right: 10px;
      }

      .detail-favi {
        display: flex;
        width: 120px;
        flex-direction: column;
        color: gray;
        align-items: center;
        border-left: 1px solid #C7C7C7;

        text {
          font-size: 12px;
        }
      }
    }

    .yf {
      padding: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }

  .goods-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>
